new Vue({
  el: '#container',
  data: {
    qiniu_url: VUE_APP_SERVER_QINIU_URL,
    coursePubId: 0,
    order: {
      orderNo: '',
      coursePubName: '',
      startTime: '',
      endTime: '',
      initialPrice: 0,
      price: 0
    }
  },
  computed: {
    validTime: function() {
      let startTime = this.order.startTime
      let endTime = this.order.endTime
      if (!startTime || !endTime) {
        return ''
      }
      return (
        '课程有效期：' +
        startTime.substring(0, 10) +
        ' - ' +
        endTime.substring(0, 10)
      )
    }
  },
  methods: {
    createQRCode(text) {
      new QRCode('qrcode', {
        text: text,
        width: 148,
        height: 148,
        colorDark: '#000000',
        colorLight: '#ffffff',
        correctLevel: QRCode.CorrectLevel.H
      })
    },
    searchOrder() {
      let interval = setInterval(() => {
        search_order(this.order.orderNo).then(data => {
          if (data.status === 600002) {
            clearInterval(interval)
            this.goToLearingArticle()
          }
        })
      }, 3000)
    },
    goToLearingArticle() {
      location.href = `${this.qiniu_url}/pages/${this.coursePubId}.html`
    }
  },
  created() {
    this.coursePubId = parseInt(localURL.searchParams.get('coursePubId'), 10)

    create_order(this.coursePubId).then(data => {
      this.order = data

      create_pay(this.order.orderNo).then(data => {
        // TODO: 判断status: 1
        this.createQRCode(data.codeURL)
        this.searchOrder()
      })
    })
  }
})
